import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// loading style
enum HUDLoadingStyle {
  light,
  dark,
  custom,
}

/// toast position
enum HUDToastPosition {
  top,
  center,
  bottom,
}

/// indicator type
enum HUDIndicatorType {
  fadingCircle,
  circle,
  threeBounce,
  chasingDots,
  wave,
  wanderingCubes,
  rotatingPlain,
  doubleBounce,
  fadingFour,
  fadingCube,
  pulse,
  cubeGrid,
  rotatingCircle,
  foldingCube,
  pumpingHeart,
  dualRing,
  hourGlass,
  pouringHourGlass,
  fadingGrid,
  ring,
  ripple,
  spinningCircle,
  squareCircle,
}

class HUDUtil {
  /// HUDUtil: Init hud.
  static TransitionBuilder init() {
    TransitionBuilder builder = EasyLoading.init();
    return builder;
  }

  /// HUDUtil: Config hud.
  static void config({
    HUDLoadingStyle loadingStyle = HUDLoadingStyle.dark,
    HUDIndicatorType indicatorType = HUDIndicatorType.circle,
    HUDToastPosition toastPosition = HUDToastPosition.bottom,
  }) {
    EasyLoading.instance
      ..userInteractions = true
      ..dismissOnTap = false
      ..loadingStyle = _getLoadingStyle(loadingStyle)
      ..indicatorType = _getIndicatorType(indicatorType)
      ..toastPosition = _getToastPosition(toastPosition)
      ..displayDuration = const Duration(milliseconds: 2500);
  }

  /// HUDUtil: Show hud loading.
  static void show(String? message, {Widget? indicator, EasyLoadingMaskType? maskType}) {
    EasyLoading.instance.userInteractions = false;
    EasyLoading.show(status: message, indicator: indicator, maskType: maskType);
  }

  /// HUDUtil: Show hud info.
  static void showInfo(String message, {Duration? duration = const Duration(milliseconds: 2500)}) {
    EasyLoading.showInfo(message, duration: duration);
  }

  /// HUDUtil: Show hud success.
  static void showSuccess(String message, {Duration? duration = const Duration(milliseconds: 2500)}) {
    EasyLoading.showSuccess(message, duration: duration);
  }

  /// HUDUtil: Show hud error.
  static void showError(String message, {Duration? duration = const Duration(milliseconds: 2500)}) {
    EasyLoading.showError(message, duration: duration);
  }

  /// HUDUtil: Show hud toast.
  static void toast(String message, {Duration? duration = const Duration(milliseconds: 2500)}) {
    EasyLoading.showToast(message, duration: duration);
  }

  /// HUDUtil: Show hud progress.
  static void showProgress(double value, {String? message}) {
    EasyLoading.showProgress(value, status: message);
  }

  /// HUDUtil: Dismiss hud.
  static void dismiss() {
    EasyLoading.instance.userInteractions = true;
    EasyLoading.dismiss();
  }

  static EasyLoadingStyle _getLoadingStyle(HUDLoadingStyle style) {
    switch (style) {
      case HUDLoadingStyle.light:
        return EasyLoadingStyle.light;
      case HUDLoadingStyle.dark:
        return EasyLoadingStyle.dark;
      case HUDLoadingStyle.custom:
        return EasyLoadingStyle.custom;
    }
  }

  static EasyLoadingToastPosition _getToastPosition(HUDToastPosition position) {
    switch (position) {
      case HUDToastPosition.top:
        return EasyLoadingToastPosition.top;
      case HUDToastPosition.center:
        return EasyLoadingToastPosition.center;
      case HUDToastPosition.bottom:
        return EasyLoadingToastPosition.bottom;
    }
  }

  static EasyLoadingIndicatorType _getIndicatorType(HUDIndicatorType style) {
    switch (style) {
      case HUDIndicatorType.fadingCircle:
        return EasyLoadingIndicatorType.fadingCircle;
      case HUDIndicatorType.circle:
        return EasyLoadingIndicatorType.circle;
      case HUDIndicatorType.threeBounce:
        return EasyLoadingIndicatorType.threeBounce;
      case HUDIndicatorType.chasingDots:
        return EasyLoadingIndicatorType.chasingDots;
      case HUDIndicatorType.wave:
        return EasyLoadingIndicatorType.wave;
      case HUDIndicatorType.wanderingCubes:
        return EasyLoadingIndicatorType.wanderingCubes;
      case HUDIndicatorType.rotatingPlain:
        return EasyLoadingIndicatorType.rotatingPlain;
      case HUDIndicatorType.doubleBounce:
        return EasyLoadingIndicatorType.doubleBounce;
      case HUDIndicatorType.fadingFour:
        return EasyLoadingIndicatorType.fadingFour;
      case HUDIndicatorType.fadingCube:
        return EasyLoadingIndicatorType.fadingCube;
      case HUDIndicatorType.pulse:
        return EasyLoadingIndicatorType.pulse;
      case HUDIndicatorType.cubeGrid:
        return EasyLoadingIndicatorType.cubeGrid;
      case HUDIndicatorType.rotatingCircle:
        return EasyLoadingIndicatorType.rotatingCircle;
      case HUDIndicatorType.foldingCube:
        return EasyLoadingIndicatorType.foldingCube;
      case HUDIndicatorType.pumpingHeart:
        return EasyLoadingIndicatorType.pumpingHeart;
      case HUDIndicatorType.dualRing:
        return EasyLoadingIndicatorType.dualRing;
      case HUDIndicatorType.hourGlass:
        return EasyLoadingIndicatorType.hourGlass;
      case HUDIndicatorType.pouringHourGlass:
        return EasyLoadingIndicatorType.pouringHourGlass;
      case HUDIndicatorType.fadingGrid:
        return EasyLoadingIndicatorType.fadingGrid;
      case HUDIndicatorType.ring:
        return EasyLoadingIndicatorType.ring;
      case HUDIndicatorType.ripple:
        return EasyLoadingIndicatorType.ripple;
      case HUDIndicatorType.spinningCircle:
        return EasyLoadingIndicatorType.spinningCircle;
      case HUDIndicatorType.squareCircle:
        return EasyLoadingIndicatorType.squareCircle;
    }
  }
}
